﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using MS.InformationManagement;
using MS.InformationManagement.Presenter.Support;
using MS.InformationManagement.Support;
using System.Collections.Generic;
using MS.InformationManagement.DAL.DocumentCenter;
using MS.InformationManagement.Presenter.DistribuirRadicado;
using Microsoft.SharePoint;
using MS.InformationManagement.Controls._CONTROLTEMPLATES.InformationManagerControls;
using MS.InformationManagement.Support.Constants;
using System.Collections;

namespace MS.InformationManagement.WP.DocumentCenter.DistribuirRadicado
{
    public partial class DistribuirRadicadoUserControl : BaseControl, IDistribuirRadicadoView
    {
        #region Properties

        public IEnumerable<RadicadoExterno> radicadosRecibidos { get; set; }
        public string Tipo { get; set; }
        public Hashtable Recibidos { get; set; }

        #endregion

        #region fields

        private DistribuirRadicadoPresenter _presenter;

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                _presenter = new DistribuirRadicadoPresenter(this);
                Page.RegisterAjaxLoaderPlugin();
                btnRecibir.AddLoader();
                Tipo = GlobalConstants.METADATA_TERM_TIPO_RADICADO_ENTRADA;
                _presenter.HandleLoad();
                //if (spgrdDItribuirRad.Rows.Count > 0)
                //    btnRecibir.Enabled = true;
                //else
                //    btnRecibir.Enabled = false;

            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }


        protected void spgrdDItribuirRad_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
        {

        }

        #region Implemented Methods

        public void Show()
        {
            spgrdDItribuirRad.DataSource = radicadosRecibidos;
            spgrdDItribuirRad.DataBind();
            if (spgrdDItribuirRad.Rows.Count > 0)
                btnRecibir.Enabled = true;
            else
                btnRecibir.Enabled = false;
        }

        public void Success()
        {
            this._ShowAlertAndClose("Los radicados han sido Recibido exitosamente");
        }

        #endregion

        protected void btnRecibir_Click(object sender, EventArgs e)
        {
            try
            {
                CheckBox chk = new CheckBox();
                Recibidos = new Hashtable();
                foreach (GridViewRow row in spgrdDItribuirRad.Rows)
                {
                    chk = (CheckBox)row.Cells[2].FindControl("chkRecibido");
                    if (chk.Checked)
                    {
                        Recibidos.Add(row.Cells[0].Text, row.Cells[4].Text);
                        chk.Checked = false;
                    }

                }

                if (Recibidos.Count == 0)
                    throw new Exception("Debe seleccionar al menos un radicado");

                _presenter.HandleSave();
                if (Recibidos.Count > 0)
                {
                    Recibidos.Clear();
                    btnRecibir.Enabled = false;
                }
                else
                    btnRecibir.Enabled = true;
                _presenter.HandleLoad();
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }

        }
    }
}
